package org.example.market.service.BillService;
import org.example.market.entity.Bill;
import org.example.market.entity.BillCountVO;
import org.example.market.entity.RecentBillAmountVO;
import org.example.market.entity.RecentBillCountVO;
import org.example.market.mapper.BillMapper;
import org.example.market.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BillService {

    @Autowired
    private BillMapper billMapper;



    public Result<Bill> addBill(Bill bill) {
        try {
            billMapper.insertBill(bill);
            return Result.success(bill);
        } catch (Exception e) {
            return Result.error(500, "添加账单失败: " + e.getMessage());
        }
    }

    public Result<PageResult<Bill>> getBillList(int currentPage, int pageSize,
                                                String productName, Integer providerId,
                                                Integer isPayment) {
        try {
            int offset = (currentPage - 1) * pageSize;
            List<Bill> bills = billMapper.findByPage(offset, pageSize, productName, providerId, isPayment);
            int total = billMapper.countByCondition(productName, providerId, isPayment);

            PageResult<Bill> pageResult = new PageResult<>();
            pageResult.setList(bills);
            pageResult.setTotal(total);
            pageResult.setCurrentPage(currentPage);
            pageResult.setPageSize(pageSize);

            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error(500, "查询账单列表失败: " + e.getMessage());
        }
    }

    public Result<Bill> updateBill(Bill bill) {
        try {
            billMapper.updateBill(bill);
            return Result.success(bill);
        } catch (Exception e) {
            return Result.error(500, "更新账单失败: " + e.getMessage());
        }
    }

    public Result<Void> deleteBill(Long id) {
        try {
            billMapper.deleteBillById(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "删除账单失败: " + e.getMessage());
        }
    }

    public Result<Bill> getBillById(Long id) {
        try {
            Bill bill = billMapper.getBillById(id);
            if (bill != null) {
                return Result.success(bill);
            }
            return Result.error(404, "未找到该账单");
        } catch (Exception e) {
            return Result.error(500, "查询账单失败: " + e.getMessage());
        }
    }

    /**
     * 获取今日、昨日、本周、本月账单数量
     * @return 封装好的结果对象
     */
    public Result<BillCountVO> getBillCounts() {
        try {
            BillCountVO billCountVO = billMapper.getBillCounts();
            return Result.success(billCountVO, "账单数量统计查询成功");
        } catch (Exception e) {
            return Result.error("账单数量统计查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取最近7天的账单数量
     * @return 封装好的结果对象
     */
    public Result<List<RecentBillCountVO>> getRecent7DaysBillCount() {
        try {
            List<RecentBillCountVO> billCountList = billMapper.getRecent7DaysBillCount();
            return Result.success(billCountList, "最近7天账单数量查询成功");
        } catch (Exception e) {
            return Result.error("最近7天账单数量查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取最近7天的账单金额
     * @return 封装好的结果对象
     */
    public Result<List<RecentBillAmountVO>> getRecent7DaysBillAmount() {
        try {
            List<RecentBillAmountVO> billAmountList =billMapper.getRecent7DaysBillAmount();
            return Result.success(billAmountList, "最近7天账单金额查询成功");
        } catch (Exception e) {
            return Result.error("最近7天账单金额查询失败：" + e.getMessage());
        }
    }
}